package com.yanqu.road.server.manger.cricket.skillhandler.attround.handler;

import afu.org.checkerframework.checker.oigj.qual.O;
import com.yanqu.road.entity.cricket.helper.AddHpResult;
import com.yanqu.road.entity.enums.eSkillType;
import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.server.gameplayer.module.cricket.battle.CricketGame;
import com.yanqu.road.server.gameplayer.module.cricket.battle.eCricketBattleRoundStep;
import com.yanqu.road.server.gameplayer.module.cricket.model.BattleData;
import com.yanqu.road.server.gameplayer.module.cricket.model.SkillData;
import com.yanqu.road.server.manger.cricket.skillhandler.attround.CricketSkillAttRoundStrategy;
import com.yanqu.road.server.manger.cricket.skillhandler.startround.CricketSkillStartRoundStrategy;
import com.yanqu.road.utils.RandomHelper;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class CricketSkill256Strategy implements CricketSkillAttRoundStrategy {

    /**
     * 技能处理-蛮荒骷的举摔变为强化版，当前气势增加N1千分比，举摔最后一段伤害附加双方气势差N2千分比伤害，最低伤害为蛮荒骷气势N3千分比。
     * @param cricketGame 本局对局对象
     * @param skillData 触发的技能
     * @param baseAttSkill 基础攻击技能
     * @param baseAttReHpResult 基础攻击结果对象
     * @param action 我方
     * @param target 对方
     * @param attackRoundBuilder 返回PB结构体
     * @return true：死亡，退出技能处理，false：继续技能处理
     */
    @Override
    public boolean skillHandle(CricketGame cricketGame, SkillData skillData, SkillData baseAttSkill, AddHpResult baseAttReHpResult, BattleData action, BattleData target, CricketProto.CricketAttackRound.Builder attackRoundBuilder, long battleFrameId) {
        //基础攻击miss，不触发技能
        if (baseAttReHpResult == null) {
            return false;
        }
        //蛮荒骷-沙漠送葬: 举摔变为强化版，当前气势增加N1千分比，举摔最后一段伤害附加双方气势差N2千分比伤害，最低伤害为蛮荒骷气势N3千分比。
        if (baseAttSkill.getSkillEffect().getSkillType() == eSkillType.CricketBattleSkill137.getValue() && baseAttSkill.getSkillEffect().getP4() == 3) {//1快夹2冲夹3举摔
            skillData.effectAmountInc();
            CricketProto.CricketBattleFrame.Builder skill256FrameBuilder = cricketGame.newBattleFrameBuilder();
            skill256FrameBuilder.setActionUserId(action.getUserId());
            skill256FrameBuilder.setTargetUserId(target.getUserId());
            skill256FrameBuilder.setSkillId(skillData.getSkillId());
            //增加气势
            long adPower = BigDecimal.valueOf(action.getPower()).multiply(BigDecimal.valueOf(skillData.getSkillEffect().getP1())).divide(BigDecimal.valueOf(1000), 8, RoundingMode.DOWN).longValue();
            action.addPower(adPower);
            skill256FrameBuilder.setAddPower(adPower);
            //伤害
            long minValue = BigDecimal.valueOf(action.getPower()).multiply(BigDecimal.valueOf(skillData.getSkillEffect().getP3())).divide(BigDecimal.valueOf(1000), 8, RoundingMode.DOWN).longValue();
            long diffPower = action.getPower() - target.getPower();
            long value = BigDecimal.valueOf(diffPower).multiply(BigDecimal.valueOf(skillData.getSkillEffect().getP2())).divide(BigDecimal.valueOf(1000), 8, RoundingMode.DOWN).longValue();
            value = Math.max(value, minValue);
            AddHpResult addHpResult = target.addHp(-value);
            skill256FrameBuilder.setAttack(addHpResult.getValue());
            skill256FrameBuilder.setIsKill(addHpResult.isKill());
            skill256FrameBuilder.setHarmEffectSkillId(addHpResult.getReduceHpEffectSkillId());
            skill256FrameBuilder.setIsMiss(addHpResult.isMissFlag());
            cricketGame.fillRemainHp(skill256FrameBuilder);
            attackRoundBuilder.addAttack(skill256FrameBuilder);
            if (cricketGame.isOver(attackRoundBuilder, eCricketBattleRoundStep.Attack.getValue(), skill256FrameBuilder.getBattleFrameId())) {
                return true;
            }
        }
        return false;
    }
}
